#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool>dp(s.size() + 1, false);
        dp[0] = true;
        for (int j = 0; j <= s.size(); j++)
        {
            for (int i = 0; i < j; i++)
            {
                string word = s.substr(i, j - i);
                if (wordSet.find(word) != wordSet.end() && dp[i])
                    dp[j] = true;
            }
        }
        return dp[s.size()];
    }
};

